//
//  DHSearchProductCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/2/10.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHSearchProductCell.h"
#import "DHProductCollectionViewCell.h"
#import "DHProductDetailViewController.h"

@interface DHSearchProductCell()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) NSArray *productAarray;

@end

@implementation DHSearchProductCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        if (!self.collectionView) {
            UICollectionViewFlowLayout *layout =  [[UICollectionViewFlowLayout alloc] init];
            self.collectionView =[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0) collectionViewLayout: layout];
            self.collectionView.backgroundColor = [UIColor clearColor];
            self.collectionView.delegate = self;
            self.collectionView.dataSource = self;
            self.collectionView.scrollEnabled = NO;
            [self.collectionView registerNib:[UINib nibWithNibName:@"DHProductCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kDHProductCollectionViewCell];
            [self addSubview:self.collectionView];
        }
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)updateCellWithProductArr:(NSArray *)productArr andCollectionViewHeight:(CGFloat)height{
    self.productAarray = productArr;
    self.collectionView.height = height;
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDataSource and UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.productAarray.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DHProductCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kDHProductCollectionViewCell forIndexPath:indexPath];
    Product *product = [Product modelWithDictionary:self.productAarray[indexPath.row]];
    [cell updateWithProduct:product];
    return cell;
}

#pragma mark ---- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return (CGSize){kScreenWidth/2,317};
}

// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 3;
}

// 两个cell之间的最小间距，是由API自动计算的，只有当间距小于该值时，cell会进行换行
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    Product *product = [Product modelWithDictionary:self.productAarray[indexPath.row]];
    DHProductDetailViewController *detailController = [[DHProductDetailViewController alloc] initWithProductID:product._id brandName:product.brand.name];
    [self.viewController.navigationController pushViewController:detailController animated:YES];
}

#pragma mark - GET AND SET
- (NSArray *)productAarray{
    if (!_productAarray) {
        _productAarray = [NSArray array];
    }
    return _productAarray;
}

@end
